import http from '../../util/request.js';
var page = 1;
var pageSize = 20;
var app = getApp();

var GetList = function(that) {
  if (that.data.hidden) {
    that.setData({
      hidden: false
    });
    http.get('article', {
      act: 'list',
      page: page,
      pageSize: pageSize
    }).then(function (res) {
      var list = that.data.list;
      if (res.length > 0) {
        if (page == 1) {
          list = res;
        } else {
          for (var i = 0; i < res.length; i++) {
            list.push(res[i]);
          }
        }
        that.setData({
          list: list
        });
        page++;
        that.setData({
          hidden: true
        });
      } else {
        that.setData({
          load_text: '没有更多数据'
        });
      }
    });
  }
}
Page({
  data: {
    hidden: true,
    list: [],
    scrollTop: 0,
    scrollHeight: 0,
    load_text: '加载中...'
  },
  onLoad: function() {
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        console.info(res.windowHeight);
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });
    that.setTabBar();
    that.getCategory();
  },
  // 获取分类列表
  getCategory: function () {
    var that = this;
    var cache = wx.getStorageSync("categoryList");
    if (cache) {
      // console.log(cache);
      that.setData({
        categoryList: cache
      });
    } else {
      http.get('category', {act: 'list'}).then(function (res) {
        if (res.length > 0) {
          wx.setStorageSync('categoryList', res);
          that.setData({
            categoryList: res
          });
        } else {
          that.setData({
            categoryList: []
          });
        }
      });
    }
  },
  setTop: function (setting) {
    var that = this;
    var backgroundColor = setting.pro.backgroundColor != undefined ? setting.pro.backgroundColor : '#000000';
    var frontColor = setting.pro.frontColor != undefined ? setting.pro.frontColor : '#ffffff';
    var currentPage = getCurrentPages();
    var page = currentPage[0].route;
    wx.setNavigationBarColor({
      frontColor: frontColor,
      backgroundColor: backgroundColor
    });
    var tarBar = that.data.blist;
    for (let key in tarBar) {
      if (tarBar[key].link == page) {
        wx.setNavigationBarTitle({
          'title': tarBar[key].title
        });
        break;
      }
    }
    that.setData({
      currentPage: page
    });
  },
  // 设置底部导航
  setTabBar: function () {
    var that = this;
    var cache = wx.getStorageSync("barlist");
    var setting = {};
    if (cache) {
      setting = cache;
      that.setData({
        blist: cache.menu,
        pro: cache.pro
      });
      that.setTop(setting);
    } else {
      http.get('quickmenu', {}).then(function (res) {
        setting = res;
        console.log(setting);
        if (res != {}) {
          that.setTop(setting);
          wx.setStorageSync('barlist', res);
          that.setData({
            blist: res.menu,
            pro: res.pro
          });
        } else {
          that.setData({
            blist: [],
            pro: []
          });
        }
      });
    }
  },
  onShow: function() {
    var that = this;
    page = 1;
    GetList(that);
  },
  bindDownLoad: function() {
    var that = this;
    GetList(that);
  },
  goToUrl: function (e) {
    var that = this;
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    })
  }
})